//
//  ViewController.m
//  Amap_demo
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 Carlos. All rights reserved.
//

#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

#import "CustomAnnotationView.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<MAMapViewDelegate, AMapSearchDelegate>
//    地图
@property (nonatomic, strong) MAMapView *mapV;
//    搜索
@property (nonatomic, strong) AMapSearchAPI *search;
//    用户位置
@property (nonatomic, retain) MAUserLocation *userLocation;


@end

@implementation ViewController

#pragma 标注1
//    大头针标注
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
//    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
//    pointAnnotation.coordinate = CLLocationCoordinate2DMake(38.88346669, 121.54480577);
//    pointAnnotation.title = @"蓝欧科技";
//    pointAnnotation.subtitle = @"数码路南段21号";
//    
//    [self.mapV addAnnotation:pointAnnotation];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [MAMapServices sharedServices].apiKey = @"424056c5330b7f6ec8d666a266b35080";
    [AMapSearchServices sharedServices].apiKey = @"424056c5330b7f6ec8d666a266b35080";
    
    self.mapV = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.mapV.delegate = self;
    
    //    3D 的三种地图类型(2D 只有前两种)
    //    卫星地图
    [self.mapV setMapType:MAMapTypeSatellite];
    
    //    标准地图
//    [self.mapV setMapType:MAMapTypeStandard];
    
    //    夜景地图
//    [self.mapV setMapType:MAMapTypeStandardNight];
    
    //    是否显示地区名字
//    [self.mapV setShowsLabels:NO];
    
    //    是否显示实时交通路况
    [self.mapV setShowTraffic:YES];
    
    //    设置比例尺位置
    [self.mapV setScaleOrigin:CGPointMake(self.mapV.scaleOrigin.x, 22)];
    
    
    [self.view addSubview:self.mapV];
    
    
    //    开始定位
    [self.mapV setShowsUserLocation:YES];
    //    地图跟着定位移动
    [self.mapV setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
    
    //    自定义定位精度圈样式
    [self.mapV setCustomizeUserLocationAccuracyCircleRepresentation:YES];
   
    
    
#pragma 位置检索
    //    初始化检索对象
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    
//    //构造AMapPOIAroundSearchRequest对象，设置周边请求参数
//    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
//    request.location = [AMapGeoPoint locationWithLatitude:38.88346669 longitude:121.54480577];
//    request.keywords = @"肯德基";
//    // types属性表示限定搜索POI的类别，默认为：餐饮服务|商务住宅|生活服务
//    // POI的类型共分为20种大类别，分别为：
//    // 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|
//    // 医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|
//    // 交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
//    request.types = @"餐饮服务|购物服务|生活服务";
//    request.sortrule = 0;
//    request.requireExtension = YES;
//    
//    [self.search AMapPOIAroundSearch:request];
    
#pragma mark - 路径规划查询
//    //    公交换乘查询    AMapTransitRouteSearchRequest
//    
//    //    步行搜索参数    AMapWalkingRouteSearchRequest
//    
//    //    驾车路径规划请求参数     AMapDrivingRouteSearchRequest
//    AMapDrivingRouteSearchRequest *request = [[AMapDrivingRouteSearchRequest alloc] init];
//    request.origin = [AMapGeoPoint locationWithLatitude:38.88346669 longitude:121.54480577];
//    request.destination = [AMapGeoPoint locationWithLatitude:38.88346669 longitude:121.54480577];
//    //    驾车导航策略
//    request.strategy = MADrivingStrategyShortest;//    距离最短
//    //    返回扩展信息
//    request.requireExtension = YES;
//    
//    [self.search AMapDrivingRouteSearch:request];
    
    
    
#pragma mark - 地理编码
    //    地址和地理坐标互相转换
    //    地理编码  地址转换成地理坐标(经纬度)
    //    逆地理编码 地理坐标转换成地址
    
#pragma 正向地理编码(地址匹配)
//    //    正向地理编码，请求参数类为 AMapGeocodeSearchRequest，address是必设参数
//    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
//    geo.address = @"彤徳莱火锅";
//    [self.search AMapGeocodeSearch:geo];
    
#pragma 逆地理编码
//    //    逆地理编码, 请求参数类为 AMapReGeocodeSearchRequest, location为必设参数
//    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
//    regeo.location = [AMapGeoPoint locationWithLatitude:38.88346669 longitude:121.54480577];
//    regeo.radius = 10000;
//    regeo.requireExtension = YES;
//    [self.search AMapReGoecodeSearch: regeo];
    
#pragma mark - 公交线路查询(有错误)
    //    可以根据线路关键字和线路ID两种方法
    
    //    根据关键字查询时 请求参数类为 AMapBusLineNameSearchRequest,keywords为必设参数
    
    //    根据线路ID查询时, 请求参数类为 AMapBusLineIDSearchRequest,uid 为必设参数
//    
//    AMapBusLineNameSearchRequest *lineRequest = [[AMapBusLineNameSearchRequest alloc] init];
//    lineRequest.keywords = @"901";
//    lineRequest.city = @"dalian";
//    lineRequest.requireExtension = YES;
//    
//    //    发起公交路线查询
//    [self.search AMapBusLineNameSearch:lineRequest];
    
#pragma 输入提示搜索
    //    输入提示是指根据用户输入的关键词，给出相应的提示信息，将最有可能的搜索词呈现给用户，以减少用户输入信息，提升用户体验
    //    输入提示返回的提示语对象 AMapTip 有多种属性，可根据该对象的返回信息，配合其他搜索服务使用，完善您应用的功能
    
    
    
    
    
}
#pragma 实现公交线路查询的回调函数
//    有错误
- (void)onBusLineSearchDone:(AMapBusLineBaseSearchRequest *)request response:(AMapBusLineSearchResponse *)response
{
    if (0 == response.buslines.count) {
        return;
    }
    //    通过  AMapBusLineSearchResponse  对象处理搜索结果
    NSString *strCount = [NSString stringWithFormat:@"count: %d", response.count];
    NSString *strLine = @"";
    for (AMapBusLine *p in response.buslines) {
        strLine = [NSString stringWithFormat:@"%@\nLine: %@", strLine, p.description];
    }
//    NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggest, strLine];
}

#pragma 逆地理编码的回调函数
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (nil != response.regeocode) {
        //    通过  AMapReGeocodeSearchResponse  对象处理搜索结果
        NSString *result = [NSString stringWithFormat:@"ReGeocode: %@", response.regeocode];
        NSLog(@"ReGeo: %@", result);
    }
}

#pragma 正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
    if (0 == response.geocodes.count) {
        return;
    }
    //    通过  AMapGeocodeSearchResponse  对象处理搜索结果
    NSString *strCount = [NSString stringWithFormat:@"count: %d", response.count];
    NSString *strGeocodes = @"";
    for (AMapTip *p in response.geocodes) {
        strGeocodes = [NSString stringWithFormat:@"%@\ngeocode: %@", strGeocodes, p.description];
    }
    NSString *result = [NSString stringWithFormat:@"%@ \n %@", strCount, strGeocodes];
    NSLog(@"Geocode: %@", result);
    
}


#pragma 实现路径搜索的回调函数
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    if (nil == response.route) {
        return;
    }
    NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route];
    NSLog(@"%@", route);
    
}

#pragma 实现POI搜索对应的回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if (0 == response.pois.count) {
        NSLog(@"没有找到");
        return;
    }
    //    通过 AmapPOISearchResponse 对象处理结果
    NSString *strCount = [NSString stringWithFormat:@"count: %d", response.count];
    NSString *strSuggestion = [NSString stringWithFormat:@"Suggestion: %@", response.suggestion];
    NSString *strPoi = @"";
    for (AMapPOI *poi in response.pois) {
        strPoi = [NSString stringWithFormat:@"%@\nPOI: %@", strPoi, poi.description];
    }
    NSString *result = [NSString stringWithFormat:@"%@ \n %@ \n %@", strCount, strSuggestion, strPoi];
    //    打印地址
    NSLog(@"Place: %@", result);
}

#pragma mark - 自定义定位精度圈
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay
{
    if (overlay == mapView.userLocationAccuracyCircle) {
        
        MACircleView *accuracyCircleView = [[MACircleView alloc] initWithCircle:overlay];
        //    笔触宽度
        accuracyCircleView.lineWidth = 2.f;
        //    笔触颜色
        accuracyCircleView.strokeColor = [UIColor lightGrayColor];
        accuracyCircleView.fillColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.3];
        return accuracyCircleView;
    }
    return nil;
}

#pragma mark - 设置标注样式/自定义定位精度
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        
        
#pragma 大头针样式
//        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
//        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
//        if (nil == annotationView) {
//            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
//        }
//        //    设置气泡可以弹出(默认为NO)
//        [annotationView setCanShowCallout:YES];
//        //    设置标注动画显示(默认为NO)
//        [annotationView setAnimatesDrop:YES];
//        //    设置标注可以拖动(默认为NO)
//        [annotationView setDraggable:YES];
//        //    设置大头针颜色(Green Purple Red三种)
//        [annotationView setPinColor:MAPinAnnotationColorPurple];
        
        
#pragma 自定义标注图标
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        //    没有提示框
//        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        //    有提示框
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (nil == annotationView)
        {
            //    没有提示框
//            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
            //    有提示框
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        annotationView.image = [UIImage imageNamed:@"restaurant"];
        
        //    有提示框时候需要
        // 设置为NO，用以调用自定义的calloutView
        annotationView.canShowCallout = NO;
        
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);

#pragma 自定义userLocation对应的annotationView
//        static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
//        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
//        if (nil == annotationView) {
//            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userLocationStyleReuseIndetifier];
//        }
//        annotationView.image = [UIImage imageNamed:@"userPosition"];
        
        return annotationView;
    }
    return nil;
}
#pragma mark - 位置更新时的调函数
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        self.userLocation = userLocation;
        
        //取出当前位置的坐标
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
